import java.sql.*;
import java.util.Scanner;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        m1();
        //m2();
       // m3();
        //m4();
        //m5();

    }

    private static void m5() {
    }

    private static void m4() throws ClassNotFoundException, SQLException {
        Connection conn=null;
        PreparedStatement preparedStatement =null;

        try{Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除：");
        int id=sc.nextInt();
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取链接
        conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8","root","200517");
        //3.编写sql语句
        String sql="delete from sys_user where id=?";
        //4.预处理sql
        preparedStatement = conn.prepareStatement(sql);
        //5.如果有参数，设置参数
        preparedStatement.setObject(1,id);
        //6.执行sql语句
        //ResultSet resultSet = preparedStatement.executeQuery();
        int result = preparedStatement.executeUpdate();
        //7.如果是查询就遍历结果/如果是增删改 判断是否成功
        if(result>0){
            System.out.println("删除成功");
        }}catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            try{
                if (preparedStatement!=null){
                    preparedStatement.close();
                }
                if (conn!=null){
                    conn.close();
                }
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }

    }

    private static void m3(){
        Connection conn=null;
        PreparedStatement preparedStatement =null;
        try{Scanner sc=new Scanner(System.in);
        System.out.println("请输入要新增的id：");
        int id=sc.nextInt();
        System.out.println("请输入要新增的姓名：");
        String name= sc.next();
        System.out.println("请输入要新增的密码：");
        String pass=sc.next();
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取链接
        conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8","root","200517");
        //3.编写sql语句
        String sql="insert into sys_user(id,name,pass)values (?,?,?)";
        //4.预处理sql
         preparedStatement = conn.prepareStatement(sql);
        //5.如果有参数，设置参数
        preparedStatement.setObject(1,id);
        preparedStatement.setObject(2,name);
        preparedStatement.setObject(3,pass);
        //6.执行sql语句
        //ResultSet resultSet = preparedStatement.executeQuery();
        int result = preparedStatement.executeUpdate();
        //7.如果是查询就遍历结果/如果是增删改 判断是否成功
        if(result>0){
            System.out.println("新增成功");
        }}catch (ClassNotFoundException e){
                throw new RuntimeException(e);
            }catch (SQLException e){
                throw new RuntimeException(e);
            }finally {
                try{
                    if (preparedStatement!=null){
                        preparedStatement.close();
                    }
                    if (conn!=null){
                        conn.close();
                    }
                }catch (SQLException e){
                    throw new RuntimeException(e);
                }
            }

    }

    private static void m2(){

        Connection conn=null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
      try{
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查询的姓名：");
        String word = sc.next();
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取链接
        conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "200517");
        //3.编写sql语句
        String sql = "select * from sys_user where name like ?";
        //4.预处理sql
        preparedStatement = conn.prepareStatement(sql);
        //5.如果有参数，设置参数
        preparedStatement.setObject(1, "%" + word + "%");
        //6.执行sql语句
        resultSet = preparedStatement.executeQuery();
        //7.如果是查询就遍历结果/如果是增删改 判断是否成功
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(id + "------" + name + "-------" + pass);
        }
    }catch (ClassNotFoundException e){
          throw new RuntimeException(e);
      }catch (SQLException e){
          throw new RuntimeException(e);
      }finally {
          try{
              if(resultSet!=null){
                  resultSet.close();
              }
              if (preparedStatement!=null){
                  preparedStatement.close();
              }
              if (conn!=null){
                  conn.close();
              }
          }catch (SQLException e){
              throw new RuntimeException(e);
          }
      }
    }

    private static void m1(){
        Connection conn=null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        //1.加载驱动
       try{ Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取链接
        conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8","root","200517");
        //3.编写sql语句
        String sql="select * from sys_user";
        //4.预处理sql
        preparedStatement = conn.prepareStatement(sql);
        //5.如果有参数，设置参数
        //6.执行sql语句
        resultSet = preparedStatement.executeQuery();
        //7.如果是查询就遍历结果/如果是增删改 判断是否成功
        while (resultSet.next()){
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(id+"------"+name+"-------"+pass);
        }
        //8.关闭资源
        resultSet.close();
        preparedStatement.close();
        conn.close();
    }catch (ClassNotFoundException e){
        throw new RuntimeException(e);
    }catch (SQLException e){
        throw new RuntimeException(e);
    }finally {
        try{
        if(resultSet!=null){
            resultSet.close();
        }
        if (preparedStatement!=null){
            preparedStatement.close();
        }
        if (conn!=null){
            conn.close();
        }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

}}